package com.lenovo.leos.filebrowser.ui;

import com.lenovo.leos.filebrowser.engine.util.FileHandler;
import com.lenovo.leos.filebrowser.engine.util.FileHandler.MsgHandleFunc;
import com.lenovo.leos.filebrowser.engine.util.FileHandler.MsgHandler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;

public class BaseActivity extends Activity {
	private static final String TAG = "BaseActivity";
	
	private FileHandler mfHandler = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Global.init(this);
		initHandler();		
	}
	
	private void initHandler() {
		if (null == mfHandler){
			mfHandler = new FileHandler(Looper.myLooper());
		}
	}
	
	public FileHandler getHandler() {
		if (null == mfHandler)
			initHandler();
		return mfHandler;
	}
	
	public void addMsgHandler(int msgId, MsgHandleFunc handler) {
		if (null == mfHandler)
			initHandler();
		if (null != mfHandler){
			mfHandler.addMsgWithHandler(msgId, handler);
		}
	}
	
	public void addMsgHandler(MsgHandler handler) {
		if (null == mfHandler)
			initHandler();
		if (null != mfHandler){
			mfHandler.addMsgHandler(handler);
		}
	}
	
	public void sendEmptyMsg(int what){
		if (null == mfHandler)
			initHandler();
		if (null != mfHandler){
			mfHandler.sendEmptyMessage(what);
		}		
	}
	
	public void sendMsg(Message msg){
		if (null == msg)
			return;
		
		if (null == mfHandler)
			initHandler();
		if (null != mfHandler){
			mfHandler.sendMessage(msg);
		}		
	}
	
	public void postRunnable(Runnable run){
		if (null == run)
			return;
		
		if (null == mfHandler)
			initHandler();
		if (null != mfHandler){
			mfHandler.post(run);
		}			
	}
	
}